package com.ccnf.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ccnf.core.utils.ResultUtil;
import com.ccnf.core.utils.SystemClock;
import com.ccnf.model.entity.InvitationCollection;
import com.ccnf.model.entity.InvitationRecord;
import com.ccnf.model.entity.InvitationUser;
import com.ccnf.model.mapper.InvitationCollectionMapper;
import com.ccnf.model.mapper.InvitationRecordMapper;
import com.ccnf.model.mapper.InvitationReportMapper;
import com.ccnf.model.mapper.InvitationUserMapper;
import com.ccnf.service.CollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CollectionServiceImpl implements CollectionService {
    @Autowired
    private InvitationCollectionMapper invitationCollectionMapper;
    @Autowired
    private InvitationRecordMapper invitationRecordMapper;
    @Autowired
    private InvitationUserMapper invitationUserMapper;
    @Autowired
    private InvitationReportMapper invitationReportMapper;

    @Override
    public void collection(InvitationCollection invitationCollection) {
        Integer isCollected = invitationCollection.getIsCollection(); // 是否收藏状态
        if (isCollected != null) {
            Long invitationId = invitationCollection.getInvitationId();// 邀请函ID
            Long userId = invitationCollection.getUserId();
//            if (userId.equals(invitation.getUserId())) {
//                throw new BizException("你不能收藏自己的邀请函");
//            }
            InvitationCollection selectInvitationCollection = new InvitationCollection();
            selectInvitationCollection.setInvitationId(invitationId);
            selectInvitationCollection.setUserId(userId);
            InvitationCollection select = invitationCollectionMapper.selectOne(selectInvitationCollection);
            if (select == null) {
                // 没有收藏记录
                if (isCollected == 1) {
                    invitationCollection.setCollectionTime(SystemClock.second());
                    ResultUtil.validateSQL(invitationCollectionMapper.insert(invitationCollection), "收藏失败");
                    handleCollectionCount(invitationId, userId);
                }
            } else if (!select.getIsCollection().equals(isCollected)){
                // 存在收藏记录，且收藏状态不同
                if (isCollected == 1) {
                    invitationCollection.setCollectionTime(SystemClock.second());
                    handleCollectionCount(invitationId, userId);
                } else {
                    InvitationRecord invitationRecord = new InvitationRecord();
                    invitationRecord.setInvitationId(invitationId);
                    invitationRecord.setRecordType(1);// 今日
                    invitationRecord = invitationRecordMapper.selectOne(invitationRecord);
                    if (invitationRecord.getCollectionCount() > 0) {
                        invitationRecordMapper.reduceCollectionCount(invitationId);
                    } else {
                        invitationRecordMapper.reduceTotalCollectionCount(invitationId);
                    }
                }
                ResultUtil.validateSQL(invitationCollectionMapper.update(invitationCollection, new EntityWrapper<>(selectInvitationCollection)),
                        isCollected == 1 ? "" : "取消" + "收藏失败");
            }
        }
    }

    /**
     * 处理收藏邀请函的统计数据
     * @param invitationId 邀请函ID
     * @param userId 用户ID
     */
    private void handleCollectionCount(Long invitationId, Long userId) {
        new Thread(() -> {
            InvitationUser invitationUser = new InvitationUser();
            invitationUser.setInvitationId(invitationId);
            invitationUser.setUserId(userId);
            invitationUser.setCollectionTime(SystemClock.second());
            invitationUserMapper.updateCollectionTime(invitationUser);// 更新邀请函用户的收藏时间

            invitationRecordMapper.incrementCollectionCount(invitationId);// 增加邀请函的收藏数
            invitationReportMapper.incrementCollectionCount(invitationId, SystemClock.zeroSecond());
        }).start();
    }

    @Override
    public boolean findCollectionStatus(Long invitationId, Long userId) {
        InvitationCollection selectInvitationCollection = new InvitationCollection();
        selectInvitationCollection.setInvitationId(invitationId);
        selectInvitationCollection.setUserId(userId);
        InvitationCollection select = invitationCollectionMapper.selectOne(selectInvitationCollection);
        return select != null && select.getIsCollection() == 1;
    }
}